import { diff } from "./diff"
// Component.js
export class Component {
  constructor(props) {
    this.props = props
  }
  setState (state) {
    this.state = Object.assign({}, this.state, state)
    /* 
    1. 获取旧vdom
    2. 获取新vdom
    3. 比对更新节点
    */
    let vdom = this.render() //1
    let oldDom = this.getDom() //2
    let container = oldDom.parentNode
    diff(vdom, container, oldDom)//3
  }
  updateProps (props) {
    this.props = props
  }
  setDom (dom) {
    this._dom = dom
  }
  getDom () {
    return this._dom
  }


  /* 
  生命周期函数(子组件可覆盖)
  组件挂载两个生命周期函数：
  1. 挂载前
  2. 挂载完成
  */
  componentWillMount () { }
  componentDidMount () { }

  /* 
  组件更新5生命周期函数：
  1. 接收最新props前
  2. 是否更新组件（优化性能）
  3. 组件将要更新
  4. 组件更新完成
  5. 组件将要卸载
  */
  // 1
  componentWillReceiveProps (nextProps) { }
  // 2
  shouldComponentUpdate (nextProps, nextState) {
    return nextProps != this.props || nextState != this.state
  }
  // 3
  componentWillUpdate (nextProps, nextState) { }
  // 4
  componentDidUpdate (prevProps, preState) { }
  // 5
  componentWillUnmount () { }
}